﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AppToolkit.WPF.Utils
{
    /// <summary>
    /// Support functionality around PackUris.
    /// </summary>
    public static class PackUriHelper
    {
        /// <summary>
        /// Helper method for generating a "pack://" URI for a given relative file based on the
        /// assembly that this class is in.
        /// </summary>
        /// <param name="relativeFile">The relative path to the component.</param>
        /// <returns>A URI to the specified component.</returns>
        public static Uri FromLocal(string relativeFile)
        {
            return From(typeof(PackUriHelper), relativeFile);
        }

        /// <summary>
        /// Helper method for generating a "pack://" URI for a given relative file based on the
        /// assembly that this class is in.
        /// </summary>
        /// <param name="typeInAssembly">type in assembly</param>
        /// <param name="relativeFile">A URI to the specified component.</param>
        /// <returns></returns>
        public static Uri From(Type typeInAssembly, string relativeFile)
        {
            string uriString = String.Format("pack://application:,,,/{0};component/{1}", 
                getShortName(typeInAssembly), relativeFile);
            return new Uri(uriString);
        }

        private static string getShortName(Type typeInAssembly)
        {
            Assembly a = typeInAssembly.Assembly;

            // Pull out the short name.
            return a.ToString().Split(',')[0];
        }
    }
}
